﻿package mortal.game.view.systemSetting
{
    import flash.events.*;
    import flash.utils.*;
    import frEngine.util.*;
    import mortal.common.global.*;
    import mortal.game.events.*;
    import mortal.game.manager.*;
    import mortal.game.mvc.*;
    import mortal.game.scene3D.layer3D.utils.*;
    import mortal.mvc.core.*;
    import mortal.mvc.interfaces.*;

    public class SystemSettingController extends Controller
    {
        private var _timer:Timer;
        private var _systemsettingModule:SystemSettingModule;

        public function SystemSettingController()
        {
            this._timer = new Timer(2000);
            return;
        }// end function

        override protected function initServer() : void
        {
            Dispatcher.addEventListener(EventName.SystemSettingSava, this.saveClientSettingHandler);
            Dispatcher.addEventListener(EventName.HideSetterChange, this.onHideSetterChange);
            Dispatcher.addEventListener(EventName.LoginVoiceSet, this.onLoginVoiceSet);
            NetDispatcher.addCmdListener(ServerCommand.SystemSettingDataGot, this.onSystemSettingUpdate);
            this._timer.addEventListener(TimerEvent.TIMER, this.onSaveTimer);
            this._timer.start();
            return;
        }// end function

        override protected function initView() : IView
        {
            if (!this._systemsettingModule)
            {
                this._systemsettingModule = new SystemSettingModule();
            }
            return this._systemsettingModule;
        }// end function

        private function onLoginVoiceSet(event:DataEvent) : void
        {
            var _loc_2:* = Boolean(event.data);
            if (SystemSetting.instance.isInitedFromServer)
            {
                SystemSetting.instance.isForbidMusic.value = _loc_2 ? (1) : (0);
                Dispatcher.dispatchEvent(new DataEvent(EventName.ForbitMusicChange, SystemSetting.instance.isForbidMusic.displayValue));
                SystemSetting.instance.save();
            }
            return;
        }// end function

        private function onSystemSettingUpdate(param1 = null) : void
        {
            if (ParamsConst.instance.isEnterSetVoice)
            {
                SystemSetting.instance.isForbidMusic.value = ParamsConst.instance.isEnterNoVoice ? (1) : (0);
                Dispatcher.dispatchEvent(new DataEvent(EventName.ForbitMusicChange, SystemSetting.instance.isForbidMusic.displayValue));
                SystemSetting.instance.save();
                ParamsConst.instance.isEnterSetVoice = false;
            }
            return;
        }// end function

        private function onSaveTimer(event:TimerEvent) : void
        {
            if (ClientSetting.local.isDirty)
            {
                ClientSetting.local.isDirty = false;
                ClientSetting.save();
            }
            return;
        }// end function

        private function onHideSetterChange(event:DataEvent) : void
        {
            switch(SystemSetting.instance.hideSetter.value)
            {
                case 0:
                {
                    SystemSetting.instance.hideSetter.value = 1;
                    break;
                }
                case 1:
                {
                    SystemSetting.instance.hideSetter.value = 2;
                    break;
                }
                case 2:
                {
                    SystemSetting.instance.hideSetter.value = 0;
                    break;
                }
                default:
                {
                    break;
                }
            }
            SystemSetting.instance.save();
            return;
        }// end function

        private function saveClientSettingHandler(event:DataEvent) : void
        {
            var _loc_2:* = int(event.data["type"]);
            var _loc_3:* = String(event.data["value"]);
            var _loc_4:* = Boolean(event.data["isNotTip"]);
            GameProxy.role.saveClientSetting(_loc_2, _loc_3);
            if (_loc_2 == SystemSettingType.SystemKeyMapData)
            {
                if (!_loc_4)
                {
                    MsgManager.showRollTipsMsg("保存成功");
                }
            }
            else if (_loc_2 == SystemSettingType.SystemSetting)
            {
                if (!_loc_4)
                {
                    MsgManager.showRollTipsMsg("保存成功");
                }
                ThingUtil.isHeadContainerChange = true;
                ThingUtil.isMoveChange = true;
                PerformanceUtil.instance.setEffectQulity(SystemSetting.instance.effectSetterType.value);
                Dispatcher.dispatchEvent(new DataEvent(EventName.SystemSettingSaved, event.data));
            }
            return;
        }// end function

    }
}
